package cn.zhf.shop.controller;

import cn.zhf.shop.entity.*;
import cn.zhf.shop.entity.count.CountNumbers;
import cn.zhf.shop.entity.organization.Organization;
import cn.zhf.shop.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@Api(tags = "统计模块")
@RequestMapping("/api/count")
public class CountController {
    @Resource
    private CountService countService;

    @Resource
    private OrganizationService organizationService;

    @Resource
    private OrdersService ordersService;

    @Resource
    private StageService stageService;

    @Resource
    private CampusService campusService;

    @Resource ConsoleUserService consoleUserService;


    @GetMapping("/count/cam/age/all")
    @ApiOperation(value = "获得所有校区的年龄分段统计")
    public Message<?> getCountAgeAll(){
        return countService.countAllAgeByCam();
    }
    @GetMapping("/count/cam/age")
    @ApiOperation(value = "获得指定校区的年龄分段统计")
    public Message<?> getCountAgeAllCamId(int camId){
        return countService.countAgeByCam(camId);
    }
    @GetMapping("/count/cam/edu/all")
    @ApiOperation(value = "获得所有校区的学历分段统计")
    public Message<?> getCountEduAll(){
        return countService.countAllEduByCam();
    }
    @GetMapping("/count/cam/edu")
    @ApiOperation(value = "获得指定校区的学历分段统计")
    public Message<?> getCountAgeEduCamId(int camId){
        return countService.countEduByCam(camId);
    }



    @GetMapping("/sum/cam/all")
    @ApiOperation(value = "获得全部校区的所有订单统计")
    public Message<?> getSumCamAll(){
        return countService.sumAllByCam();
    }
    @GetMapping("/sum/cam")
    @ApiOperation(value = "获得指定校区的所有订单统计")
    public Message<?> getSumCam(int camId){
        return countService.sumByCam(camId);
    }
    @GetMapping("/sum/cam/stage/all")
    @ApiOperation(value = "获得所有校区的分期订单统计")
    public Message<?> getSumCamAllStage(){
        return countService.sumAllByCamS();
    }


    @GetMapping("/sum/cam/stage")
    @ApiOperation(value = "获得指定校区的分期订单统计")
    public Message<?> getSumCamStage(int camId){
        return countService.sumByCamS(camId);
    }
    @GetMapping("/sum/cam/nstage/all")
    @ApiOperation(value = "获得所有校区的直接付款订单统计")
    public Message<?> getSumCamAllNStage(){
        return countService.sumAllByCamNS();
    }
    @GetMapping("/sum/cam/nstage")
    @ApiOperation(value = "获得指定校区的直接付款订单统计")
    public Message<?> getSumCamNStage(int camId){
        return countService.sumByCamNS(camId);
    }



    @GetMapping("/sum/org/all")
    @ApiOperation(value = "获得全部校区的所有订单统计")
    public Message<?> getSumOrgAll(){
        return countService.sumAllByOrg();
    }
    @GetMapping("/sum/org")
    @ApiOperation(value = "获得指定校区的所有订单统计")
    public Message<?> getSumOrg(int camId){
        return countService.sumByOrg(camId);
    }

    @GetMapping("/sum/org/stage/all")
    @ApiOperation(value = "获得所有校区的分期订单统计")
    public Message<?> getSumOrgAllStage(){
        return countService.sumAllByOrgS();
    }
    @GetMapping("/sum/org/stage")
    @ApiOperation(value = "获得指定校区的分期订单统计")
    public Message<?> getSumOrgStage(int orgId){
        return countService.sumByOrgS(orgId);
    }
    @GetMapping("/sum/org/nstage/all")
    @ApiOperation(value = "获得所有校区的直接付款订单统计")
    public Message<?> getSumOrgAllNStage(){
        return countService.sumAllByOrgNS();
    }
    @GetMapping("/sum/org/nstage")
    @ApiOperation(value = "获得指定校区的直接付款订单统计")
    public Message<?> getSumOrgNStage(int orgId){
        return countService.sumByOrgNS(orgId);
    }

    @GetMapping("/sum/all")
    @ApiOperation(value = "总的数据")
    public Message<?> countAll(){
        return countService.countAll();
    }

    @ApiOperation(value = "推荐人推荐的所有订单")
    @GetMapping("/referenceId/allOrders")
    public Message<CountNumber> getReferenceIdallOrders(int referenceId){
        List<Organization> organizationList = organizationService.selectByReferenceId(referenceId);
        int price=0;
        int number=0;
        for(int i=0;i<organizationList.size();i++)
        {
            String orgName = organizationList.get(i).getOrgName();
            number = number+ordersService.countByOrgName(orgName);
            List<BigDecimal> bigDecimalList = ordersService.selectTotalPriceByOrgNameAndState(orgName,1);
            for (int j=0;j<bigDecimalList.size();j++)
            {
                price=price+bigDecimalList.get(j).intValue();
            }
        }
        CountNumber countNumber = new CountNumber();
        countNumber.setNumber(number);
        countNumber.setPrice(price);
        return Message.success(null).add(countNumber);
    }

    @ApiOperation(value = "推荐人推荐的分期订单")
    @GetMapping("/referenceId/allStageOrders")
    public Message<CountNumber> getReferenceIdallStageOrders(int referenceId){
        List<Organization> organizationList = organizationService.selectByReferenceId(referenceId);
        int price=0;
        int number=0;
        for (int i=0;i<organizationList.size();i++)
        {
            String orgName = organizationList.get(i).getOrgName();
            List<Orders> ordersList = ordersService.selectByOrgNameAndState(orgName,1);
            for (int j=0;j<ordersList.size();j++)
            {
                String orderId = ordersList.get(j).getId();
                if (stageService.getStageByOrderId2(orderId)!=null)
                {
                    number++;
                    price=price+ordersList.get(j).getTotalPrice().intValue();
                }
            }
        }
        CountNumber countNumber = new CountNumber();
        countNumber.setNumber(number);
        countNumber.setPrice(price);
        return Message.success(null).add(countNumber);
    }

    @ApiOperation(value = "推荐人推荐的直接付款订单")
    @GetMapping("/referenceId/allNoStageOrders")
    public Message<CountNumber> getReferenceIdallNoStageOrders(int referenceId){
        List<Organization> organizationList = organizationService.selectByReferenceId(referenceId);
        int price=0;
        int number=0;
        for (int i=0;i<organizationList.size();i++)
        {
            String orgName = organizationList.get(i).getOrgName();
            List<Orders> ordersList = ordersService.selectByOrgNameAndState(orgName,1);
            for (int j=0;j<ordersList.size();j++)
            {
                String orderId = ordersList.get(j).getId();
                if (stageService.getStageByOrderId2(orderId)==null)
                {
                    number++;
                    price=price+ordersList.get(j).getTotalPrice().intValue();
                }
            }
        }
        CountNumber countNumber = new CountNumber();
        countNumber.setNumber(number);
        countNumber.setPrice(price);
        return Message.success(null).add(countNumber);
    }

    /**
     * 获得指定校区的所有订单统计，分期订单统计，直接付款订单统计。统计包括（总价格+订单数量）
     */
    @ApiOperation(value = "获得指定校区的所有订单统计，分期订单统计，直接付款订单统计。统计包括（总价格+订单数量）")
    @GetMapping("/Campus/allMoney")
    public Message<CountNumber1> CampusAllMoney(int camId){
        /**
         * 所有总价
         */
       int allprice=0;
        /**
         * 所有订单数量
         */
        int allnumber=0;
        /**
         * 分期订单总价
         */
        int fenqiprice=0;
        /**
         * 分期订单订单数量
         */
        int fenqinumber=0;
         /**
         * 直接付款订单总价
         */
       int zhijieprice=0;
        /**
         * 直接付款订单数量
         */
        int zhijienumber=0;

        Campus campus = campusService.selectByPrimaryKey2(camId);
        List<Orders> ordersList = ordersService.getByCampusName2(campus.getCampusName());
        for (int i=0;i<ordersList.size();i++)
        {
            allnumber++;
            allprice=allprice+ordersList.get(i).getTotalPrice().intValue();
            if (stageService.getStageByOrderId2(ordersList.get(i).getId())!=null){
                fenqinumber++;
                fenqiprice=fenqiprice+ordersList.get(i).getTotalPrice().intValue();
            }else{
                zhijienumber++;
                zhijieprice=zhijieprice+ordersList.get(i).getTotalPrice().intValue();
            }
        }
        CountNumber1 countNumber1 = new CountNumber1();
        countNumber1.setAllnumber(allnumber);
        countNumber1.setAllprice(allprice);
        countNumber1.setFenqinumber(fenqinumber);
        countNumber1.setFenqiprice(fenqiprice);
        countNumber1.setZhijienumber(zhijienumber);
        countNumber1.setZhijieprice(zhijieprice);
        return Message.success(null).add(countNumber1);
    }

    /**
     *推荐人能分到的钱
     */
    @ApiOperation(value = "推荐人能分到的钱")
    @GetMapping("/countMoney/referenceRatio")
    public Message countMoneyreferenceRatio(int referenceId){
        List<Organization> organizationList = organizationService.selectByReferenceId(referenceId);
        double price=0;
        for (int i=0;i<organizationList.size();i++)
        {
            String orgName = organizationList.get(i).getOrgName();
            Double ratio = organizationList.get(i).getReferenceRatio();
            List<Orders> ordersList = ordersService.selectByOrgNameAndState(orgName,1);
            for (int j=0;j<ordersList.size();j++)
            {
                    price=price+ordersList.get(j).getTotalPrice().doubleValue()*ratio;
            }
        }
        return Message.success(null).add(price);
    }

    /**
     *某一个机构系统能分到的钱
     */
    @ApiOperation(value = "某一个机构系统能分到的钱")
    @GetMapping("/countMoney/one/shareRatio")
    public Message countMoneyOneshareRatio(int orgId){
        Organization organization = organizationService.selectByPrimaryKey(orgId).getObj();
        double price=0;
        String orgName = organization.getOrgName();
            Double shareRatio=organization.getShareRatio();
            List<Orders> ordersList = ordersService.selectByOrgNameAndState(orgName,1);
            for (int j=0;j<ordersList.size();j++)
            {
                price=price+ordersList.get(j).getTotalPrice().doubleValue()*shareRatio;
            }

        return Message.success(null).add(price);
    }

    /**
     *系统能分到的钱
     */
    @ApiOperation(value = "所有机构系统能分到的钱")
    @GetMapping("/countMoney/All/shareRatio")
    public Message countMoneyAllshareRatio(){
        List<Organization> organizationList = organizationService.selectAll2();
        double price=0;
        for (int i=0;i<organizationList.size();i++)
        {
         String orgName = organizationList.get(i).getOrgName();
        Double shareRatio=organizationList.get(i).getShareRatio();
        List<Orders> ordersList = ordersService.selectByOrgNameAndState(orgName,1);
        for (int j=0;j<ordersList.size();j++)
        {
            price=price+ordersList.get(j).getTotalPrice().doubleValue()*shareRatio;
        }

        }
        return Message.success(null).add(price);
    }

    /**
     * 图表统计
     * time:1234分别是周月季年
     * orderId和campusId为空的值为-999

     * @return
     */
    @ApiOperation(value = "图表统计")
    @GetMapping("/chart/count")
    public Message countchart(CountNumbers countNumbers) throws ParseException {

        return countService.countchart(countNumbers);
    }

    @ApiOperation(value = "导出excel")
    @GetMapping("/getExcel")
    public Message getExcel(int orgId,int campusId,int timetype,String searchdate) throws ParseException {
        return countService.getExcel(orgId, campusId,timetype,searchdate);
    }

    @ApiOperation(value = "机构所需的统计信息")
    @GetMapping("/orgCount")
    public Message orgCount(String orgName){
        return Message.success(null).add(countService.orgCount(orgName));
    }

    @ApiOperation(value = "代理人业务")
    @GetMapping("/consoleUser/coutorder")
    public Message coutconsoleUserorder(int userid,String date1,String date2) throws ParseException {
        return countService.coutconsoleUserorder(userid, date1, date2);
    }
    @ApiOperation(value = "代理人业务2")
    @GetMapping("/consoleUser/coutorder2")
    public Message coutconsoleUserorder2(int userid,String date1,String date2) throws ParseException {
        return countService.coutconsoleUserorder2(userid, date1, date2);
    }
    @ApiOperation(value = "直辖代理人业务")
    @GetMapping("/zhixiaconsoleUser/coutorder")
    public Message coutzhixiaconsoleUserorder(int userid,String date1,String date2) throws ParseException {
        return countService.coutzhixiaconsoleUserorder(userid, date1, date2);
    }
    @ApiOperation(value = "直辖代理人业务2")
    @GetMapping("/zhixiaconsoleUser/coutorder2")
    public Message coutzhixiaconsoleUserorder2(int userid,String date1,String date2) throws ParseException {
        return countService.coutzhixiaconsoleUserorder2(userid, date1, date2);
    }

    @ApiOperation(value = "所有代理人业务")
    @GetMapping("/allconsoleUser/coutorder")
    public Message coutallconsoleUserorder(String date1,String date2) throws ParseException {
        return countService.coutallconsoleUserorder(date1, date2);
    }
    @ApiOperation(value = "所有代理人业务2")
    @GetMapping("/allconsoleUser/coutorder2")
    public Message coutallconsoleUserorder2(String date1,String date2) throws ParseException {
        return countService.coutallconsoleUserorder2(date1, date2);
    }


    /**
     *	财务统计
     */
    @ApiOperation(value = "财务统计")
    @GetMapping("/countFinance")
    public Message countFinance(int timeType,String date1) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = simpleDateFormat.parse(date1);
        return countService.countFinance(timeType,date);
    }









}
